//package com.company.ems.filter;
//
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
//import java.io.IOException;
//
//@WebFilter({
//        "/dashboard",
//        "/employee/*"
//})
//public class AuthenticationFilter implements Filter {
//
//    @Override
//    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
//            throws IOException, ServletException {
//
//        HttpServletRequest req = (HttpServletRequest) request;
//        HttpServletResponse resp = (HttpServletResponse) response;
//        HttpSession session = req.getSession(false);
//
//        // 检查用户是否已登录
//        if (session == null || session.getAttribute("user") == null) {
//            // 如果是 AJAX 请求，返回 401
//            if ("XMLHttpRequest".equals(req.getHeader("X-Requested-With"))) {
//                resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
//                resp.getWriter().write("{\"error\": \"unauthorized\"}");
//            } else {
//                // 普通请求，重定向到登录页
//                resp.sendRedirect("login");
//            }
//            return;
//        }
//
//        chain.doFilter(request, response);
//    }
//}
